package webapi

import (
	"MqttCloudProtocolServer/publiclib/logiclib"
	"fmt"
	"net/http"
	"runtime"
	"strconv"
	"strings"
)

//ServerWatchManagerService 网关服务器注册API类。注册控制台
type ServerWatchManagerService struct {
	base ServiceBase
}

//Regist 注册
func (_this *ServerWatchManagerService) Regist(mux *http.ServeMux) {
	_this.base.Regist(mux)
	_this.base.path += "swsm"
	_this.base.HandleRegist("getStatues", _this.getStatues)
	_this.base.HandleRegist("setDaemon", _this.setDaemon)
	_this.base.HandleRegist("controlProcess", _this.controlProcess)
	_this.base.HandleRegist("getServerOS", _this.getServerOS)
}

//getStatues 获取状态
func (_this *ServerWatchManagerService) getStatues(w http.ResponseWriter, r *http.Request) {

	client := new(logiclib.RPCClientManager)
	client.Init()
	client.Start()
	respDaemon := false
	respProtocol := false
	respMqtt := false
	respWeb := false
	if obj := client.GetInfo(&logiclib.CMDStruct{Name: "Daemon", Command: "STATUES", Param: ""}); obj != nil {
		respDaemon = obj.Response == "Running"
	}
	if obj := client.GetInfo(&logiclib.CMDStruct{Name: "EmqxServer", Command: "STATUES", Param: ""}); obj != nil {
		respMqtt = obj.Response == "Running"
	}
	if obj := client.GetInfo(&logiclib.CMDStruct{Name: "ProtocolServer", Command: "STATUES", Param: ""}); obj != nil {
		respProtocol = obj.Response == "Running"
	}
	if obj := client.GetInfo(&logiclib.CMDStruct{Name: "WebServer", Command: "STATUES", Param: ""}); obj != nil {
		respWeb = obj.Response == "Running"
	}

	res := "{\"res\":{\"daemon\":" + strconv.FormatBool(respDaemon) + ", \"protocol\":" + strconv.FormatBool(respProtocol) + ", \"mqtt\":" + strconv.FormatBool(respMqtt) + ", \"web\":" + strconv.FormatBool(respWeb) + "}}"
	fmt.Fprintln(w, res)
}

//setDaemon 设置守护
func (_this *ServerWatchManagerService) setDaemon(w http.ResponseWriter, r *http.Request) {
	vars := r.URL.Query()
	if len(vars) > 0 {
		control := GetParam(vars, "control")
		//_this.daemonStatue, _ = strconv.ParseBool(control)
		client := new(logiclib.RPCClientManager)
		client.Init()
		client.Start()
		resp := client.GetInfo(&logiclib.CMDStruct{Name: "Daemon", Command: "DAEMON", Param: strings.ToUpper(control)})
		if resp.Result == "OK" {
			fmt.Fprintln(w, "Success")
		} else {
			fmt.Fprintln(w, "Failed")
		}

	} else {
		fmt.Fprintln(w, "参数错误 error")

	}
}

//controlProcess 控制进程
func (_this *ServerWatchManagerService) controlProcess(w http.ResponseWriter, r *http.Request) {
	vars := r.URL.Query()
	client := new(logiclib.RPCClientManager)
	client.Init()
	client.Start()
	if len(vars) > 0 {
		control := GetParam(vars, "control")
		processName := GetParam(vars, "processName")
		con := "STOP"
		if r, _ := strconv.ParseBool(control); r {
			con = "START"
		}
		resp := client.GetInfo(&logiclib.CMDStruct{Name: processName, Command: con, Param: strings.ToUpper(control)})
		if resp.Result == "OK" {
			fmt.Fprintln(w, "Success")
		} else {
			fmt.Fprintln(w, "Failed")
		}
	} else {
		fmt.Fprintln(w, "参数错误 error")

	}
}

//查询服务器操作系统版本。对Linux版本出于安全架构考虑不支持遥控服务启停
func (_this *ServerWatchManagerService) getServerOS(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, runtime.GOOS)
}
